package com.purong.purong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.purong.purong.R;
import com.purong.purong.bean.BankLoanBean;
import com.purong.purong.util.CommonUtil;
import com.yao.sdk.glide.GlideUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Circle Photo 列表的数据适配器
 */
public class HomeBankLoanAdapter extends RecyclerView.Adapter<HomeBankLoanAdapter.ViewHolder> {


    private List<BankLoanBean> loanList = new ArrayList<>();

    private Context mContext;
    private OnLoanClickListener mListener;
    public HomeBankLoanAdapter(Context context) {
        this.mContext = context;
    }
    public void setOnLoanClickListener(OnLoanClickListener listener){
        this.mListener=listener;
    }
    public void setData(List<BankLoanBean> creditCardList) {
//        if (Utils.isEmpty(imageList)) {
//            return;
//        }
        this.loanList = creditCardList;
    }


    /**
     * 通过ViewHolder可以访问每个item的所有控件
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        //绑定每个CircleItem的控件引用到ViewHolder
        private ImageView img;
        private LinearLayout mainLay;
        private TextView name,applyNum,note,rate,tagOne,tagTwo,tagThree;
        private View cutLine;
        public ViewHolder(View v) {
            super(v);
            img=(ImageView)v.findViewById(R.id.img);
            name=(TextView)v.findViewById(R.id.name);
            applyNum=(TextView)v.findViewById(R.id.apply_num);
            note=(TextView)v.findViewById(R.id.note);
            rate=(TextView)v.findViewById(R.id.total_rate);
            cutLine=(View)v.findViewById(R.id.cut_line);
            mainLay=(LinearLayout)v.findViewById(R.id.main_lay);
            tagOne=(TextView)v.findViewById(R.id.tag_one);
            tagTwo=(TextView)v.findViewById(R.id.tag_two);
            tagThree=(TextView)v.findViewById(R.id.tag_three);


        }
    }


    // 创建每个Item的视图 (invoked by the item_circle manager)
    @Override
    public HomeBankLoanAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                             int viewType) {
        //创建circleItem所对应的控件
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.bank_loan_item, parent, false);
        // set the view's size, margins, paddings and item_circle parameters
        ViewHolder vh = new ViewHolder(v);

        return vh;
    }

    // Replace the contents of a view (invoked by the item_circle manager)
    @Override
    public void onBindViewHolder(final ViewHolder mHolder, final int position) {
        mHolder.mainLay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener!=null){
                    mListener.onLoanClick(loanList.get(position));
                }
            }
        });
        GlideUtil.getInstance().load(mContext,loanList.get(position).getLoan_product_url(),mHolder.img,true);
        mHolder.name.setText(loanList.get(position).getLoan_product_name());
        mHolder.rate.setText(loanList.get(position).getTotal_rate()+"%/月");
        mHolder.note.setText(loanList.get(position).getLoan_product_note());
        mHolder.applyNum.setText(CommonUtil.numConvert(Integer.valueOf(loanList.get(position).getLoan_apply_num()),1)+"人申请");

        for (int i = 0; i <loanList.get(position).getLoan_tag().size() ; i++) {
            if (i==0){
                mHolder.tagOne.setVisibility(View.VISIBLE);
                mHolder.tagOne.setText(loanList.get(position).getLoan_tag().get(0));
            }else if (i==1){
                mHolder.tagTwo.setVisibility(View.VISIBLE);
                mHolder.tagTwo.setText(loanList.get(position).getLoan_tag().get(1));
            }else if (i==2){
                mHolder.tagThree.setVisibility(View.VISIBLE);
                mHolder.tagThree.setText(loanList.get(position).getLoan_tag().get(2));
            }
        }
        if (position!=loanList.size()-1){
            mHolder.cutLine.setVisibility(View.VISIBLE);
        }else {
            mHolder.cutLine.setVisibility(View.GONE);

        }
    }



    // 获取Circle数据的item个数
    @Override
    public int getItemCount() {
        return loanList.size();
    }

    public interface OnLoanClickListener{
        void onLoanClick(BankLoanBean loan);
    }
}


